<?php
/**
 *
 * User: feikor
 * Date: 2023/06/12
 */

namespace app\admin\service;

use app\model\Actionlog;
use think\facade\Db;

class ActionlogService
{
    private $model;

    public function __construct(Actionlog $model)
    {
        $this->model = $model;
    }
    public function getList($params)
    {
        $page = $params['page'] ?? 1;
        $limit = $params['limit'] ?? 10;

        $where = [];
        if (!empty($params['video_id'])) {
            $where[] = ['video_id', '=', $params['video_id']];
            $where[] = ['content', '=', '下载'];
        }
        if (!empty($params['local_id'])) {
            $where[] = ['local_id', '=', $params['local_id']];
        }
        if (!empty($params['start_date'])) {
            $where[] = ['created_at', '>=', strtotime($params['start_date'] . ' 00:00:00')];
        }
        if (!empty($params['end_date'])) {
            $where[] = ['created_at', '<=', strtotime($params['end_date'] . ' 23:59:59')];
        }
        $data = $this->model->getPagingDataByWhere($where,$page,$limit,'*','id desc');
        $admin = Db::name('admin')->column('nickname', 'id');
        foreach ($data['data'] as $key => $value) {
            $data['data'][$key]['adminname'] = $admin[$value['local_id']];
        }


        return $data;
    }
    // 获取视频列表
    public function getStatisticsList($params)
    {
        $page = $params['page'] ?? 1;
        $limit = $params['limit'] ?? 10;

        $where = [];
        if (!empty($params['local_id'])) {
            $where[] = ['local_id', '=', $params['local_id']];
        }
        if (!empty($params['start_date'])) {
            $where[] = ['created_at', '>=', strtotime($params['start_date'] . ' 00:00:00')];
        }
        if (!empty($params['end_date'])) {
            $where[] = ['created_at', '<=', strtotime($params['end_date'] . ' 23:59:59')];
        }
        $total = $this->model->where($where)->group('today')->count();

        if ($total == 0) {
            return ['total' => 0,
                'total_page' => 0,
                'data' => []
            ];
        }
        $offset = ($page - 1) * $limit;
        $totalPage = ceil($total / $limit);
        $data = $this->model->where($where)->group('today')->order('today desc')->limit($offset, $limit)->select()->toArray();
        foreach ($data as $key => $value) {

            //日期
            $data[$key]['today'] = $value['today'];
            //上传数
            $data[$key]['uploading'] = Db::name('local_video')->where($where)->where('created_at','>=',strtotime($value['today'] . ' 00:00:00'))->where('created_at','<=',strtotime($value['today'] . ' 23:59:59'))->count();
            //已过审（原生）
            $data[$key]['passed_1'] = $this->model->where($where)->where(['status' => 1, 'type' => 1, 'videotype' => 1])->where('today',$value['today'])->count();
            //已过审（衍生）
            $data[$key]['passed_2'] = $this->model->where($where)->where(['status' => 1, 'type' => 1, 'videotype' => 2])->where('today',$value['today'])->count();
            //过审总数
            $data[$key]['passed_sum'] = $this->model->where($where)->where(['status' => 1, 'type' => 1])->where('today',$value['today'])->count();
            //驳回
            $data[$key]['reject'] = $this->model->where($where)->where(['status' => 1, 'type' => 2])->where('today',$value['today'])->count();

            //合格率
            $data[$key]['percent_pass'] = $data[$key]['uploading'] == 0 ? '0%' : round($data[$key]['passed_sum'] / $data[$key]['uploading'] * 100, 2) . '%';;
        }

        return ['total' => $total,
            'total_page' => $totalPage,
            'data' => $data
        ];

        return $data;
    }

    // 获取视频列表
    public function getPerformanceList($params)
    {
        $page = $params['page'] ?? 1;
        $limit = $params['limit'] ?? 10;

        $where = [];
        $where[] = ['type', '=', '4'];
        if (!empty($params['local_id'])) {
            $where[] = ['local_id', '=', $params['local_id']];
        }
        if (!empty($params['start_date'])) {
            $where[] = ['created_at', '>=', strtotime($params['start_date'] . ' 00:00:00')];
        }
        if (!empty($params['end_date'])) {
            $where[] = ['created_at', '<=', strtotime($params['end_date'] . ' 23:59:59')];
        }
        if (!empty($params['name'])) {
            $where[] = ['video_id', 'in', Db::name('local_video')->where('name', 'like', '%' . $params['name'] . '%')->column('id')];

        }
        $total = $this->model->where($where)->count();

        if ($total == 0) {
            return ['total' => 0,
                'total_page' => 0,
                'data' => []
            ];
        }
        $offset = ($page - 1) * $limit;
        $totalPage = ceil($total / $limit);
        $video = Db::name('local_video')->column('name', 'id');
        $data = $this->model->where($where)->order('created_at desc')->limit($offset, $limit)->select()->toArray();
        foreach ($data as $key => $value) {
            //日期
            $data[$key]['name'] =isset($video[$value['video_id']])?$video[$value['video_id']]:'ID：'.$value['video_id'].'已删' ;
            $data[$key]['performance'] = floor(100.25*100)/100;
        }

        return ['total' => $total,
            'total_page' => $totalPage,
            'data' => $data
        ];

        return $data;
    }

}